package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPatentPublishBinding;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.PublishPatentDetailEntity;
import com.sgb.kjwl.utils.GlideEngine;
import com.sgb.kjwl.utils.KeyboardUtils;
import com.sgb.kjwl.utils.PickerHelper;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.utils.UploadCheckHelper;
import com.sgb.kjwl.utils.YoungDateUtils;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverPublishActivity;
import com.sgb.kjwl.viewmodel.enterprise.PublishViewModel;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PatentPublishFragment extends AppFragment {

    FragmentPatentPublishBinding mBinding;
    PublishViewModel mViewModel;
    private String mCategory, mCategoryName, mId = "", mIndustry, mIndustryName;
    private int mTalentStatus,mStatus;
    private long mStartTime,mEndTime;
    OptionsPickerView<IpPickSelectEntity> mPatentTypeOptionsPickerView, mPatentClassOptionsPickerView;
    private MakeOverPublishActivity mActivity;
    private List<File> list = new ArrayList<>();
    private String mImage = "";
    private boolean isChecked,isAutoCheck,isNoAutoCheck = true,isCheckStatusNo,isCheckStatusIn;
    Bitmap bmp;
    private int position1;
    private int position2;

    public static PatentPublishFragment newInstance() {
        PatentPublishFragment tradePublishFragment = new PatentPublishFragment();
        return tradePublishFragment;
    }

    public static PatentPublishFragment newInstance(PublishPatentDetailEntity entity) {
        PatentPublishFragment tradePublishFragment = new PatentPublishFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", entity);
        tradePublishFragment.setArguments(bundle);
        return tradePublishFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_patent_publish, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(PublishViewModel.class);

        initCheckBox();
        initButton();
        initData();
        initEdit();
    }

    private void initEdit() {
        mBinding.etFragmentPatentPublishRegisterNumEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    InputMethodManager manager = ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE));
                    manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });
        mBinding.etFragmentPatentPublishNameEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    InputMethodManager manager = ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE));
                    manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });
    }

    private void initCheckBox() {
        mBinding.ivTag3.setImageResource(R.mipmap.yigouxuan_makeover_publish);
        mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
        mBinding.tvFragmentPatentPublishUploadPictureTags.setVisibility(View.VISIBLE);
        mBinding.llFragmentTradePublishPictureTags.setVisibility(View.GONE);
        isAutoCheck = false;
        isNoAutoCheck = true;
        mBinding.deleteTv.setVisibility(View.GONE);

        mBinding.rbFragmentPatentPublishMakeoverSelectorPrice.setOnClickListener(v -> {
            isChecked = !isChecked;
            if (isChecked) {
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setEnabled(false);
                mBinding.ivYijia.setImageResource(R.mipmap.gouxuan_makeover_publish_price);
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setText("请输入价格");
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setTextColor(getResources().getColor(R.color.color_C0C4CC));
            } else {
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setEnabled(true);
                mBinding.ivYijia.setImageResource(R.mipmap.weigouxuan_makeover_publish_price);
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setText("");
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setTextColor(getResources().getColor(R.color.FF000000));
            }
        });

        mBinding.ivTag3.setImageResource(R.mipmap.yigouxuan_makeover_publish);
        isNoAutoCheck = true;
        mBinding.tvFragmentPatentPublishUpload.setOnClickListener(v -> {
            isNoAutoCheck = !isNoAutoCheck;

            if (isNoAutoCheck){
                mBinding.ivTag3.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                mBinding.tvFragmentPatentPublishUploadPictureTags.setVisibility(View.VISIBLE);
                mBinding.llFragmentTradePublishPictureTags.setVisibility(View.GONE);
                isAutoCheck = false;
            }
//            else {
//                mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
//            }
        });

        mBinding.tvFragmentPatentPublishAutoUpload.setOnClickListener(v -> {
            isAutoCheck = !isAutoCheck;
            if (isAutoCheck){
                if (mBinding.tvFragmentTradePublishSelectType.getText().toString().equals("")){
                    MToast.showToast(getContext(),"请选择专利类型");
                    isAutoCheck = false;
                    return;
                }else {
                    switch (mCategoryName){
                        case "发明专利":
                            mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish);
                            bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish);
                            break;
                        case "实用新型":
                            mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.shiyongxinxing_patent_publish);
                            bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.shiyongxinxing_patent_publish);
                            break;
                        case "外观设计":
                            mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.waiguansheji_patent_publish);
                            bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.waiguansheji_patent_publish);
                            break;
                    }
                }
                mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                mBinding.ivTag4.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                mBinding.tvFragmentPatentPublishUploadPictureTags.setVisibility(View.GONE);
                mBinding.llFragmentTradePublishPictureTags.setVisibility(View.VISIBLE);
                isNoAutoCheck = false;
            }
//            else {
//                mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
//            }

        });

        mBinding.tvFragmentPatentPublishStatusNot.setOnClickListener(v -> {
            mTalentStatus = 1;
            isCheckStatusNo = !isCheckStatusNo;
            if (isCheckStatusNo){
                mBinding.ivTag1.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                isCheckStatusIn = false;
            }else {
                mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
            }
        });

        mBinding.tvFragmentPatentPublishStatusIn.setOnClickListener(v -> {
            mTalentStatus = 2;
            isCheckStatusIn = !isCheckStatusIn;
            if (isCheckStatusIn){
                mBinding.ivTag1.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                mBinding.ivTag5.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                isCheckStatusNo = false;
            }else {
                mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
            }
        });

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity.mLiveData.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean){
                    if (!UploadCheckHelper.textCheck(getContext(), new TextView[]{mBinding.etFragmentPatentPublishNameEdit, mBinding.etFragmentPatentPublishRegisterNumEdit
                            , mBinding.tvFragmentTradePublishSelectType, mBinding.etFragmentPatentPublishAreaTypeEdit, mBinding.tvFragmentPatentPublishMakeoverMuchEdit
                            , mBinding.tvFragmentTradePublishLinkman, mBinding.tvFragmentTradePublishPhone}, new String[]{"专利名称", "专利注册号", "专利类型", "领域分类", "价格", "联系人姓名"
                            , "联系电话"})) {
                        return;
                    }

                    if(mBinding.tvFragmentPatentPublishMakeoverMuchEdit.isEnabled()&&Double.parseDouble(mBinding.tvFragmentPatentPublishMakeoverMuchEdit.getText().toString())==0){
                        MToast.showToast(getContext(), "交易价格大于0");
                    }

                    if (!isCheckStatusIn && !isCheckStatusNo){
                        MToast.showToast(getContext(),"请选择专利状态");
                        return;
                    }
//                    if (isAutoCheck){
//                        list.add(mActivity.saveBitmapFile(bmp, Environment.getExternalStorageDirectory()+"02.jpg"));
//                    }

                    if (isNoAutoCheck && mImage.equals("")  && (list == null || list.size() <= 0)){
                        MToast.showToast(getContext(),"请上传专利图片");
                        return;
                    }

                    if (isNoAutoCheck && mImage.equals("")) {
                        mViewModel.uploadPicture(getContext(), list);
                    }else {
                        upload();
                    }
                }
            }
        });
    }

    private void initButton() {
        mBinding.tvFragmentTradePublishSelectType.setOnClickListener(this);
        mBinding.etFragmentPatentPublishAreaTypeEdit.setOnClickListener(this);
        mBinding.llApplyTime.setOnClickListener(this);
        mBinding.llEndTime.setOnClickListener(this);
        mBinding.ivFragmentPatentPublishUploadPicture.setOnClickListener(this);
        mBinding.ivApplyTime.setOnClickListener(this);
        mBinding.ivEndTime.setOnClickListener(this);
        mBinding.deleteTv.setOnClickListener(this);
    }

    private void initData() {
        PublishPatentDetailEntity entity = null;
        if (getArguments() != null) {
            entity = (PublishPatentDetailEntity) getArguments().getSerializable("entity");
        }

        if (entity != null) {
            mId = entity.getId() + "";
            mCategoryName = entity.getCategoryName();
            mImage = entity.getImg();
            mIndustryName = entity.getIndustryName();
            mStatus = entity.getStatus();
            mTalentStatus = entity.getTalentStatus();
            mStartTime = entity.getApplyTime();
            mEndTime = entity.getExpiresTime();

            mBinding.tvFragmentTradePublishSelectType.setText(entity.getCategoryName());
            mBinding.etFragmentPatentPublishNameEdit.setText(entity.getName());
            mBinding.etFragmentPatentPublishRegisterNumEdit.setText(entity.getRegistrationNo());
            mBinding.tvFragmentPatentPublishIntroductionEdit.setText(entity.getExplain());
            mBinding.tvFragmentTradePublishLinkman.setText(entity.getContacts());
            mBinding.tvFragmentTradePublishPhone.setText(entity.getContactNumber());
            mBinding.etFragmentPatentPublishAreaTypeEdit.setText(entity.getIndustryName());
            mBinding.tvFragmentPatentPublishStartTimeSelect.setText(YoungDateUtils.timestampToDate(entity.getApplyTime()));
            mBinding.tvFragmentPatentPublishEndTimeSelect.setText(YoungDateUtils.timestampToDate(entity.getExpiresTime()));

            if (!mBinding.tvFragmentPatentPublishStartTimeSelect.getText().toString().equals("")){
                mBinding.ivApplyTime.setVisibility(View.VISIBLE);
            }

            if (!mBinding.tvFragmentPatentPublishEndTimeSelect.getText().toString().equals("")){
                mBinding.ivEndTime.setVisibility(View.VISIBLE);
            }


            if (entity.getTalentStatus() == 1){
                mTalentStatus = 1;
                mBinding.ivTag1.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                mBinding.ivTag5.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                isCheckStatusIn = false;
                isCheckStatusNo = true;
            }else {
                mTalentStatus = 2;
                mBinding.ivTag1.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                mBinding.ivTag5.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                isCheckStatusNo = false;
                isCheckStatusIn = true;
            }

            if (entity.getPrice() == -1) {
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setEnabled(false);
                mBinding.ivYijia.setImageResource(R.mipmap.gouxuan_makeover_publish_price);
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setText("请输入价格");
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setTextColor(getResources().getColor(R.color.color_C0C4CC));
                isChecked = true;
            } else {
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setEnabled(true);
                mBinding.ivYijia.setImageResource(R.mipmap.weigouxuan_makeover_publish_price);
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setText(PointUtils.originalPrice(entity.getPrice()));
                mBinding.tvFragmentPatentPublishMakeoverMuchEdit.setTextColor(getResources().getColor(R.color.FF000000));
                isChecked = false;
            }

            if (entity.getImg() == null || entity.getImg().equals("")){
                isAutoCheck = true;
                isNoAutoCheck = false;
                mBinding.ivTag4.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                mBinding.tvFragmentPatentPublishUploadPictureTags.setVisibility(View.GONE);
                mBinding.llFragmentTradePublishPictureTags.setVisibility(View.VISIBLE);
                mBinding.ivTag3.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                switch (entity.getCategoryName()){
                    case "发明专利":
                        mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish);
                        bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish);
                        break;
                    case "实用新型":
                        mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.shiyongxinxing_patent_publish);
                        bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.shiyongxinxing_patent_publish);
                        break;
                    case "外观设计":
                        mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.waiguansheji_patent_publish);
                        bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.waiguansheji_patent_publish);
                        break;
                }
            }else {
                isNoAutoCheck = true;
                isAutoCheck = false;
                mBinding.ivTag3.setImageResource(R.mipmap.yigouxuan_makeover_publish);
                mBinding.tvFragmentPatentPublishUploadPictureTags.setVisibility(View.VISIBLE);
                mBinding.llFragmentTradePublishPictureTags.setVisibility(View.GONE);
                mBinding.ivTag4.setImageResource(R.mipmap.weigouxuan_makeover_publish);
                Glide.with(getContext()).load(entity.getImg()).apply(new RequestOptions()
                        .placeholder(R.mipmap.makeover_add_picture)
                        .fallback( R.mipmap.makeover_add_picture)
                        .error(R.mipmap.makeover_add_picture)).into(mBinding.ivFragmentPatentPublishUploadPicture);
            }
        }

        mViewModel.getPatentTypeOption(getContext());
        mViewModel.getTerritoryTypeOption(getContext());
        mViewModel.mPatentClassListLiveData.observe(getViewLifecycleOwner(), ipPickSelectEntities -> {
            for (int i = 0; i < ipPickSelectEntities.size(); i++) {
                if (ipPickSelectEntities.get(i).getName().equals(mBinding.etFragmentPatentPublishAreaTypeEdit.getText().toString())) {
                    position1 = i;
                }
            }
            if (mPatentClassOptionsPickerView == null) {
                mPatentClassOptionsPickerView = new OptionsPickerBuilder(getContext(), (options1, options2, options3, v) -> {
                    mBinding.etFragmentPatentPublishAreaTypeEdit.setText(ipPickSelectEntities.get(options1).getName());
                    mIndustry = ipPickSelectEntities.get(options1).getId();
                    mIndustryName = ipPickSelectEntities.get(options1).getName();
                }).setCancelText("取消").setSubCalSize(15)
                        .setCancelColor(Color.parseColor("#666666"))
                        .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                        .setSubmitText("确定")
                        .setSubmitColor(Color.parseColor("#5792FD"))
                        .setTextColorCenter(Color.parseColor("#ff333333"))
                        .setContentTextSize(15)
                        .setLineSpacingMultiplier(2.5f)
                        .setDividerColor(Color.parseColor("#EDEDED"))
                        .setDecorView(getActivity().getWindow().getDecorView().findViewById(android.R.id.content))
                        .build();
                mPatentClassOptionsPickerView.setPicker(ipPickSelectEntities);
                mPatentClassOptionsPickerView.setSelectOptions(position1);
                mPatentClassOptionsPickerView.setTitleText("选择领域分类");
            }
            KeyboardUtils.hideSoftInput(getActivity());
        });

        mViewModel.mPatentTypeListLiveData.observe(getViewLifecycleOwner(), ipPickSelectEntities -> {
            for (int i = 0; i < ipPickSelectEntities.size(); i++) {
                if (ipPickSelectEntities.get(i).getName().equals(mBinding.tvFragmentTradePublishSelectType.getText().toString())) {
                    position2 = i;
                }
            }
            if (mPatentTypeOptionsPickerView == null) {
                mPatentTypeOptionsPickerView = new OptionsPickerBuilder(getContext(), (options1, options2, options3, v) -> {
                    mBinding.tvFragmentTradePublishSelectType.setText(ipPickSelectEntities.get(options1).getName());
                    mCategory = ipPickSelectEntities.get(options1).getId();
                    mCategoryName = ipPickSelectEntities.get(options1).getName();
                    switch (mCategoryName){
                        case "发明专利":
                            mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish);
                            bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish);
                            break;
                        case "实用新型":
                            mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.shiyongxinxing_patent_publish);
                            bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.shiyongxinxing_patent_publish);
                            break;
                        case "外观设计":
                            mBinding.tvFragmentTradePublishUploadPicture.setImageResource(R.mipmap.waiguansheji_patent_publish);
                            bmp= BitmapFactory.decodeResource(getResources(), R.mipmap.waiguansheji_patent_publish);
                            break;
                    }
                }).setCancelText("取消").setSubCalSize(15)
                        .setCancelColor(Color.parseColor("#666666"))
                        .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                        .setSubmitText("确定")
                        .setSubmitColor(Color.parseColor("#5792FD"))
                        .setTextColorCenter(Color.parseColor("#ff333333"))
                        .setContentTextSize(15)
                        .setLineSpacingMultiplier(2.5f)
                        .setDividerColor(Color.parseColor("#EDEDED"))
                        .setDecorView(getActivity().getWindow().getDecorView().findViewById(android.R.id.content))
                        .build();
                mPatentTypeOptionsPickerView.setPicker(ipPickSelectEntities);
                mPatentTypeOptionsPickerView.setSelectOptions(position2);
                mPatentTypeOptionsPickerView.setTitleText("选择专利类型");
            }
            KeyboardUtils.hideSoftInput(getActivity());
        });

        mActivity = (MakeOverPublishActivity) getActivity();

        mViewModel.uploadFileSuccess.observe(getViewLifecycleOwner(), list -> {
            mImage = list.get(0);
            upload();
        });

        mViewModel.mSaveSuccessPatent.observe(getViewLifecycleOwner(), aBoolean -> {
//            getActivity().setResult(1);
            EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_CREATE_NEWMAKEOVERDATA).setObject(1));
            getActivity().finish();
//            ActivityJump.jumpToActivity(getContext(), MakeOverPublishMainActivity.class, Pair.create("position", 1));
        });
    }

    private void initFileSelector(){
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .selectionMode(PictureConfig.SINGLE)
                .loadImageEngine(GlideEngine.createGlideEngine())
                .compress(true)//是否压缩
                .minimumCompressSize(4 * 1024)//小于4M不压缩
                .maxSelectNum(1)//判断还能选多少张图片
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_fragment_trade_publish_select_type:
                if (mPatentTypeOptionsPickerView != null){
                    mPatentTypeOptionsPickerView.show();
                }else {
                    mViewModel.getPatentTypeOption(getContext());
                }
                break;
            case R.id.et_fragment_patent_publish_area_type_edit:
                if (mPatentClassOptionsPickerView != null){
                    mPatentClassOptionsPickerView.show();
                }else {
                    mViewModel.getTerritoryTypeOption(getContext());
                }
                break;
            case R.id.ll_apply_time:
                PickerHelper.with(getContext()).getCommonTimeBuild("", new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        if(mEndTime>0&&DateUtils.timestampToDate(mEndTime).equals(DateUtils.timestampToDate(date.getTime()))){
                            MToast.showToast(getContext(),"申请时间不能等于到期时间");
                            return;
                        }
                        mStartTime = date.getTime();
                        mBinding.tvFragmentPatentPublishStartTimeSelect.setText(DateUtils.timestampToDate(mStartTime));
                        mBinding.ivApplyTime.setVisibility(View.VISIBLE);
                    }
                }).setRangDate(null, Calendar.getInstance()).setCancelText("取消").setSubCalSize(15)
                        .setCancelColor(Color.parseColor("#666666"))
                        .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                        .setSubmitText("确定")
                        .setSubmitColor(Color.parseColor("#5792FD"))
                        .setTextColorCenter(Color.parseColor("#ff333333"))
                        .setContentTextSize(15)
                        .setLineSpacingMultiplier(2.5f)
                        .setDividerColor(Color.parseColor("#EDEDED"))
                        .build().show();
                break;
            case R.id.ll_end_time:
                PickerHelper.with(getContext()).getCommonTimeBuild("", new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        if(mStartTime>0&&DateUtils.timestampToDate(mStartTime).equals(DateUtils.timestampToDate(date.getTime()))){
                            MToast.showToast(getContext(),"到期时间不能等于申请时间");
                            return;
                        }
                        mEndTime = date.getTime();
                        mBinding.tvFragmentPatentPublishEndTimeSelect.setText(DateUtils.timestampToDate(mEndTime));
                        mBinding.ivEndTime.setVisibility(View.VISIBLE);
                    }
                }).setRangDate(Calendar.getInstance(), null).setCancelText("取消").setSubCalSize(15)
                        .setCancelColor(Color.parseColor("#666666"))
                        .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                        .setSubmitText("确定")
                        .setSubmitColor(Color.parseColor("#5792FD"))
                        .setTextColorCenter(Color.parseColor("#ff333333"))
                        .setContentTextSize(15)
                        .setLineSpacingMultiplier(2.5f)
                        .setDividerColor(Color.parseColor("#EDEDED"))
                        .build()
                        .show();
                break;
            case R.id.iv_apply_time:
                mStartTime = 0;
                mBinding.tvFragmentPatentPublishStartTimeSelect.setText("");
                mBinding.ivApplyTime.setVisibility(View.GONE);
                break;
            case R.id.iv_end_time:
                mEndTime = 0;
                mBinding.tvFragmentPatentPublishEndTimeSelect.setText("");
                mBinding.ivEndTime.setVisibility(View.GONE);
                break;
            case R.id.iv_fragment_patent_publish_upload_picture:
                initFileSelector();
                break;
            case R.id.delete_tv:
                mBinding.ivFragmentPatentPublishUploadPicture.setImageResource(R.mipmap.makeover_add_picture);
                list.clear();
                break;
        }
    }

    public void upload() {
//        if (!CheckUtils.isMobile(mBinding.tvFragmentTradePublishPhone.getText().toString())){
//            MToast.showToast(getContext(),"请输入正确的电话格式");
//            return;
//        }

        String companyNo = "0";
        String teamNo = "0";
        String user = "0";
        if (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0) == 1) {
            companyNo = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "");
        } else if (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0) == 2) {
            teamNo = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "");
        } else {
            user = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, "");
        }

        if(mStartTime!=0&&mEndTime!=0&&DateUtils.timestampToDate(mStartTime).equals(DateUtils.timestampToDate(mEndTime))){
            MToast.showToast(getContext(),"申请时间不能等于到期时间");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("category", mCategory);
        map.put("categoryName", mCategoryName);
        map.put("industry", mIndustry);
        map.put("industryName", mIndustryName);
        map.put("companyNo", companyNo);
        map.put("contactNumber", mBinding.tvFragmentTradePublishPhone.getText().toString());
        map.put("contacts", mBinding.tvFragmentTradePublishLinkman.getText().toString());
        map.put("applyTime", mStartTime);
        map.put("id", mId);
        map.put("img", isAutoCheck ? "" : mImage);
        map.put("name", mBinding.etFragmentPatentPublishNameEdit.getText().toString());
        map.put("price", mBinding.tvFragmentPatentPublishMakeoverMuchEdit.getText().toString().equals("请输入价格")?"-1":mBinding.tvFragmentPatentPublishMakeoverMuchEdit.getText().toString());
        map.put("publishType", SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0));
        map.put("expiresTime", mEndTime);
        if (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE,0) == 0) {
            map.put("realName",SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME,""));
        } else {
            map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NAME, ""));
        }
        map.put("registrationNo", mBinding.etFragmentPatentPublishRegisterNumEdit.getText().toString());
        map.put("teamNo", teamNo);
        map.put("telPhone", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LOGIN_PHONE, ""));
        map.put("explain", mBinding.tvFragmentPatentPublishIntroductionEdit.getText().toString());
        map.put("talentStatus", mTalentStatus);
        map.put("status",mStatus);
        map.put("userNo", user);
        mViewModel.saveAndUpdatePatent(getContext(), map);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            list.clear();
            List<LocalMedia> imageList = PictureSelector.obtainMultipleResult(data);
            if (imageList == null || imageList.size() == 0) return;
            mImage = "";
            //获取压缩图片
            for (LocalMedia localMedia : imageList) {
                list.add(new File(localMedia.getCompressPath()));
            }

            Glide.with(getContext()).load(list.get(0)).into(mBinding.ivFragmentPatentPublishUploadPicture);
            mBinding.deleteTv.setVisibility(View.VISIBLE);
        }

    }

}
